library(ggplot2)
library(dplyr)
library(modelsummary)
library(kableExtra)
library(stringr)
library(fixest)
library(stargazer)
library(gridExtra)
library(flextable)

survey_long <- readRDS("data/df_long.rds")


########### Descriptives

# Number of observations
length(unique(survey_long$meta_UUID))

# Number of evaluated parties
length(unique(survey_long$evaluated_party))
sum(is.na(survey_long$feeling_toward))
sd(survey_long$feeling_toward)


desc_table <- survey_long %>% ungroup() %>%
  filter(!duplicated(meta_UUID)) %>%
  select(dem_female, dem_age, dem_rural, dem_university, lrscale,
         feeling_toward, close_num, d_affiliated, high_income, 
         trust_in_opposing_parties) %>% 
  mutate_all(as.numeric)

##### Declare covariates ####
vars <- colnames(desc_table)[colnames(desc_table) != "meta_Weight"] 
#weights = "meta_Weight"

##### Define helper function #####
# get_stats returns mean and sd for defined covariates (vars) and subgroups (treats)
get_stats <- function(var, data) {
  c(mean(data[, var] %>% unlist(), na.rm = T), 
    sd(data[, var] %>% unlist(), na.rm = T), 
    min(data[, var] %>% unlist(), na.rm = T), 
    max(data[, var] %>% unlist(), na.rm = T))
}

sapply(vars, function(x) {
  get_stats(x, data = desc_table)
})


## loop
btable <- t(sapply(vars, function(x) {
  get_stats(x, data = desc_table)
}))

## to df, add n, colnames
btable <- data.frame(do.call(cbind, list(btable)))

## times 100
perc <- c("dem_female", "dem_rural", "dem_university", "d_affiliated", "high_income")
btable[rownames(btable) %in% perc, ] <- btable[rownames(btable) %in% perc, ] * 100

## add varnames
btable$variable <- rownames(btable)

## add n obs
n <- sapply(vars, function(x) sum(!is.na(desc_table[, x])))
btable <- cbind(n, btable)

## change colnames
colnames(btable) <- c("N", "Mean", "SD", "Min", "Max", "Variable")
btable[, -which(colnames(btable) == "Variable")] <- data.frame(round(btable[, -which(colnames(btable) == "Variable")], 1))

btable$Mean[btable$Variable == "feeling_toward"] <- mean(survey_long$feeling_toward)
btable$SD[btable$Variable == "feeling_toward"] <- sd(survey_long$feeling_toward)
btable$N[btable$Variable == "feeling_toward"] <-sum(!is.na(survey_long$feeling_toward))
btable$Max[btable$Variable == "feeling_toward"] <- max(survey_long$feeling_toward)
btable$Min[btable$Variable == "feeling_toward"] <- min(survey_long$feeling_toward)

btable$Mean[btable$Variable == "close_num"] <- mean(survey_long$close_num, na.rm = T)
btable$SD[btable$Variable == "close_num"] <- sd(survey_long$close_num, na.rm = T)
btable$N[btable$Variable == "close_num"] <-sum(!is.na(survey_long$close_num))
btable$Max[btable$Variable == "close_num"] <- max(survey_long$close_num, na.rm = T)
btable$Min[btable$Variable == "close_num"] <- min(survey_long$close_num, na.rm = T)

##### Latex output via stargazer
#stargazer(btable)
## change colnames
btable$Variable <- btable$Variable %>% 
  recode("dem_female" = "Female (in %)", 
         "dem_age" = "Age", 
         "dem_rural" = "Rural (in %)", 
         "dem_university" = "University Education (in %)", 
         "close_num" = "Social distance", 
         "feeling_toward" = "Feeling score", 
         "lrscale" = "Left-right scale", 
         "d_affiliated" = "Has party affiliation (in %)",
         "high_income" = "High HH-income (in %)", 
         "trust_in_opposing_parties" = "Trust in opp. Parties (1-5)")

rownames(btable) <- NULL
btable <- btable[, c(6, 2, 3, 1, 4, 5)]


##### HTML output via stargazer
stargazer(btable, summary =F, type = "html", out = "tables/TableA1.html", rownames = F, digits = 1)
